#include "controllerstatus.h"

ControllerStatus::ControllerStatus(QObject *parent) : QObject(parent)
{
    serialThread = new QThread();
    serialPort = new ControllerSerialPort();
    serialPort->moveToThread(serialThread);
    connect(serialThread,&QThread::finished,serialThread,&QThread::deleteLater);
    connect(serialThread,&QThread::finished,serialPort,&ControllerSerialPort::deleteLater);
    connect(serialThread,&QThread::started,serialPort,&ControllerSerialPort::process);
    connect(serialPort,&ControllerSerialPort::finished,serialThread,&QThread::quit);

    connect(serialPort,&ControllerSerialPort::analyzeData,this,&ControllerStatus::analyzeData);

    connect(this,&ControllerStatus::openDoorSignal,serialPort,&ControllerSerialPort::openDoor);
    connect(this,&ControllerStatus::closeDoorSignal,serialPort,&ControllerSerialPort::closeDoor);
    serialThread->start();

    //init
    m_doorStatus = false;  //门开关传感器
    m_sWaterTankLow = false;   //小水箱高位传感器
    m_sWaterTankHigh = false;  //小水箱低位传感器
    m_bWaterTankLow = false;   //大水箱高位传感器
    m_bWaterTankHigh = false;  //大水箱地位传感器

    m_bWaterTankTemp = 0;   //大水箱温度
    m_sWaterTankTemp = 0;   //小水箱温度

    m_pumpA = 0;    //添加液泵A档位
    m_pumpB = 0;    //添加液泵B档位
    m_heaterA = false; //烘干加热棒1
    m_heaterB = false; //烘干加热棒2
    m_sHeater = false; //小水箱加热棒
    m_bHeaterA = false;    //大水箱加热棒1
    m_bHeaterB = false;    //大水箱加热棒2

    m_bWaterTankSwitch = false;    //大水箱水泵开关
    m_sWaterTankSwitch = false;    //小水箱水泵开关
    m_bInValve = false;    //大水箱进水阀门
    m_sInValve = false;    //小水箱进水阀门
    m_bOutValve = false;   //大水箱排水阀门
    m_doorSwitch = false;  //门锁开关

    m_annexA = false;
    m_annexB = false;
}

void ControllerStatus::analyzeData(QByteArray data)
{
    qDebug()<<ByteArrayToHexString(data);
    if((uint8_t)data[0] != 0xAA)
    {
        return;
    }
    int data_len = data.count();
    if((uint8_t)data[data_len-1] != 0xEE)
    {
        return;
    }
    switch ((uint8_t)data[3]) {
    case 0xA1:  //返回配置数据
        if(data[1] == 0x06)  //执行成功
        {
            //requestTimer->start(1000);
            //reComandTimer->stop();
            //qDebug()<<"sdsd"<<ByteArrayToHexString(data);
        }
        else
        {
            qCritical()<<"执行流程失败"+ByteArrayToHexString(data);
            //QMessageBox::warning(NULL, "执行失败", "执行失败！\n", QMessageBox::Yes, QMessageBox::Yes);
        }
        break;
    case 0xA2:  //任务状态信息
        {
            /*SWMTaskStatus swmTaskStatus;
            int task_id = swmTaskStatus.getFirstTaskIDFromStatus("正在执行");
            SWMTask swmTask;
            int suq = swmTask.getSequence(task_id, "执行中");
            int model_id = swmTask.getModel(task_id, suq);
            if(model_id != (int)data[4])
            {
                //QMessageBox::warning(NULL, "执行流程错误", "执行流程错误！\n", QMessageBox::Yes, QMessageBox::Yes);
                qCritical()<<"执行流程错误"+ByteArrayToHexString(data);
            }
            else
            {
                if((int)data[5] == 0)
                {
                    //当前流程完成
                    requestTimer->stop();
                    swmTask.updateStatus(task_id, suq, "已执行");
                    int suqNext = swmTask.hasNextSequence(task_id, suq+1);
                    if(suqNext > 0) //执行下一流程
                    {
                        lastsuqTimer->start(10000);
                        swmTask.updateStatus(task_id, suqNext, "执行中");
                        lastID = task_id;
                        lastSuqID = suqNext;
                    }
                    else    //任务完成
                    {
                        swmTaskStatus.updateTaskStatus(task_id, "执行完成");
                        emit sendTaskFinish(task_id);
                    }
                }
                else {
                    //时间
                    int time_ = swmTask.getTime(task_id, suq);
                    int timeDone = time_-(int)data[5] + swmTask.getTimeFromIdDone(task_id);
                    emit updateTime(timeDone);
                }
            }*/
        }
        break;
    case 0xB1:  //读取下位机各输入/输出状态
        setAnnexA((uint8_t)data[5]&0x20);
        setAnnexB((uint8_t)data[5]&0x40);
        setDoorStatus((uint8_t)data[5]&0x10);
        setSWaterTankHigh((uint8_t)data[5]&0x08);
        setSWaterTankLow((uint8_t)data[5]&0x04);
        setBWaterTankHigh((uint8_t)data[5]&0x02);
        setBWaterTankLow((uint8_t)data[5]&0x01);
        setBWaterTankTemp((uint8_t)data[6]);
        setSWaterTankTemp((uint8_t)data[7]);
        setPumpA(((uint8_t)data[8]&0xC0)>>6);   //添加液泵A档位
        setPumpB(((uint8_t)data[8]&0x30)>>4);   //添加液泵B档位
        setHeaterA((uint8_t)data[8]&0x02);  //烘干加热棒1
        setHeaterB((uint8_t)data[8]&0x04);  //烘干加热棒2
        setSHeater((uint8_t)data[8]&0x01);  //小水箱加热棒
        setBHeaterA((uint8_t)data[9]&0x40);    //大水箱加热棒1
        setBHeaterB((uint8_t)data[9]&0x80);    //大水箱加热棒2
        setBWaterTankSwitch((uint8_t)data[9]&0x20);    //大水箱水泵开关
        setSWaterTankSwitch((uint8_t)data[9]&0x10);    //小水箱水泵开关
        setBInValve((uint8_t)data[9]&0x04);    //大水箱进水阀门
        setSInValve((uint8_t)data[9]&0x08);    //小水箱进水阀门
        setBOutValve((uint8_t)data[9]&0x02);  //大水箱排水阀门
        setDoorSwitch((uint8_t)data[9]&0x01);    //门锁开关
        //emit ioStatusChanged();
        break;
    case 0xC1:  //停止任务命令
        //lastsuqTimer->stop();
        break;
    default:
        break;
    }
}

void ControllerStatus::closeDoor()
{
    emit closeDoorSignal();
}

void ControllerStatus::openDoor()
{
    emit openDoorSignal();
}

void ControllerStatus::startTask(QByteArray task)
{
    qDebug()<<"task"<<ByteArrayToHexString(task);
}

QByteArray ControllerStatus::HexStringToByteArray(QString HexString)
{
    bool ok;
    QByteArray ret;
    HexString = HexString.trimmed();
    HexString = HexString.simplified();
    QStringList sl = HexString.split(" ");

    foreach (QString s, sl) {
        if(!s.isEmpty())
        {
            //char c = s.toInt(&ok,16)&0xFF;
            int c_int = s.toInt(&ok,16);
            if(ok && (c_int<256)){
                ret.append(c_int);
            }else{
                //QMessageBox::warning(0,tr("错误："),QString("非法的16进制字符: \"%1\"").arg(s));
            }
        }
    }
    return ret;
}

QString ControllerStatus::ByteArrayToHexString(QByteArray data)
{
    QString ret(data.toHex().toUpper());
    int len = ret.length()/2;
    for(int i=1;i<len;i++)
    {
        ret.insert(2*i+i-1," ");
    }
    return ret;
}

void ControllerStatus::setDoorStatus(bool doorStatus_)
{
    if(m_doorStatus != doorStatus_)
    {
        m_doorStatus = doorStatus_;
        emit doorStatusChanged(m_doorStatus);
    }
}

void ControllerStatus::setSWaterTankLow(bool sWaterTankLow_)
{
    if(m_sWaterTankLow != sWaterTankLow_)
    {
        m_sWaterTankLow = sWaterTankLow_;
        emit sWaterTankLowChanged(m_sWaterTankLow);
    }
}

void ControllerStatus::setSWaterTankHigh(bool sWaterTankHigh_)
{
    if(m_sWaterTankHigh != sWaterTankHigh_)
    {
        m_sWaterTankHigh = sWaterTankHigh_;
        emit sWaterTankHighChanged(m_sWaterTankHigh);
    }
}

void ControllerStatus::setBWaterTankLow(bool bWaterTankLow_)
{
    if(m_bWaterTankLow != bWaterTankLow_)
    {
        m_bWaterTankLow = bWaterTankLow_;
        emit bWaterTankLowChanged(m_bWaterTankLow);
    }
}

void ControllerStatus::setBWaterTankHigh(bool bWaterTankHigh_)
{
    if(m_bWaterTankHigh != bWaterTankHigh_)
    {
        m_bWaterTankHigh = bWaterTankHigh_;
        emit bWaterTankHighChanged(m_bWaterTankHigh);
    }
}

//大水箱温度
void ControllerStatus::setBWaterTankTemp(int bWaterTankTemp_)
{
    if(m_bWaterTankTemp != bWaterTankTemp_)
    {
        m_bWaterTankTemp = bWaterTankTemp_;
        emit bWaterTankTempChanged(m_bWaterTankTemp);
    }
}

//小水箱温度
void ControllerStatus::setSWaterTankTemp(int sWaterTankTemp_)
{
    if(m_sWaterTankTemp != sWaterTankTemp_)
    {
        m_sWaterTankTemp = sWaterTankTemp_;
        emit sWaterTankTempChanged(m_sWaterTankTemp);
    }
}

//添加液泵A档位
void ControllerStatus::setPumpA(int pumpA_)
{
    if(m_pumpA != pumpA_)
    {
        m_pumpA = pumpA_;
        emit pumpAChanged(m_pumpA);
    }
}

//添加液泵B档位
void ControllerStatus::setPumpB(int pumpB_)
{
    if(m_pumpB != pumpB_)
    {
        m_pumpB = pumpB_;
        emit pumpBChanged(m_pumpB);
    }
}

//烘干加热棒1
void ControllerStatus::setHeaterA(bool heaterA_)
{
    if(m_heaterA != heaterA_)
    {
        m_heaterA = heaterA_;
        emit heaterAChanged(m_heaterA);
    }
}

//烘干加热棒2
void ControllerStatus::setHeaterB(bool heaterB_)
{
    if(m_heaterB != heaterB_)
    {
        m_heaterB = heaterB_;
        emit heaterBChanged(m_heaterB);
    }
}

//小水箱加热棒
void ControllerStatus::setSHeater(bool sHeater_)
{
    if(m_sHeater != sHeater_)
    {
        m_sHeater = sHeater_;
        emit sHeaterChanged(m_sHeater);
    }
}

//大水箱加热棒1
void ControllerStatus::setBHeaterA(bool bHeaterA_)
{
    if(m_bHeaterA != bHeaterA_)
    {
        m_bHeaterA = bHeaterA_;
        emit bHeaterAChanged(m_bHeaterA);
    }
}

//大水箱加热棒2
void ControllerStatus::setBHeaterB(bool bHeaterB_)
{
    if(m_bHeaterB != bHeaterB_)
    {
        m_bHeaterB = bHeaterB_;
        emit bHeaterBChanged(m_bHeaterB);
    }
}

//大水箱水泵开关
void ControllerStatus::setBWaterTankSwitch(bool bWaterTankSwitch_)
{
    if(m_bWaterTankSwitch != bWaterTankSwitch_)
    {
        m_bWaterTankSwitch = bWaterTankSwitch_;
        emit bWaterTankSwitchChanged(m_bWaterTankSwitch);
    }
}

//小水箱水泵开关
void ControllerStatus::setSWaterTankSwitch(bool sWaterTankSwitch_)
{
    if(m_sWaterTankSwitch != sWaterTankSwitch_)
    {
        m_sWaterTankSwitch = sWaterTankSwitch_;
        emit sWaterTankSwitchChanged(m_sWaterTankSwitch);
    }
}

//小水箱进水阀门
void ControllerStatus::setBInValve(bool bInValve_)
{
    if(m_bInValve != bInValve_)
    {
        m_bInValve = bInValve_;
        emit bInValveChanged(m_bInValve);
    }
}

//大水箱进水阀门
void ControllerStatus::setSInValve(bool sInValve_)
{
    if(m_sInValve != sInValve_)
    {
        m_sInValve = sInValve_;
        emit sInValveChanged(m_sInValve);
    }
}

//大水箱排水阀门
void ControllerStatus::setBOutValve(bool bOutValve_)
{
    if(m_bOutValve != bOutValve_)
    {
        m_bOutValve = bOutValve_;
        emit bOutValveChanged(m_bOutValve);
    }
}

//门锁开关
void ControllerStatus::setDoorSwitch(bool doorSwitch_)
{
    if(m_doorSwitch != doorSwitch_)
    {
        m_doorSwitch = doorSwitch_;
        emit doorSwitchChanged(m_doorSwitch);
    }
}

//添加液量A
void ControllerStatus::setAnnexA(bool annexA_)
{
    if(m_annexA != annexA_)
    {
        m_annexA = annexA_;
        emit annexAChanged(m_annexA);
    }
}

//添加液量B
void ControllerStatus::setAnnexB(bool annexB_)
{
    if(m_annexB != annexB_)
    {
        m_annexB = annexB_;
        emit annexBChanged(m_annexB);
    }
}

void ControllerStatus::setCurrentModeID(int currentModeID_)
{
    if(m_currentModeID != currentModeID_)
    {
        m_currentModeID = currentModeID_;
        emit currentModeIDChanged(m_currentModeID);
    }
}
